<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>《机器学习》知识点整理与展示</title>

    <!-- 调用static静态模板 -->
    <!--Bootstrap Stylesheet [ REQUIRED ]-->
    <link href="../static/css/bootstrap.min.css" rel="stylesheet">
    <link rel="shortcut icon" href="../static/images/logo.jpg">

    <!--Nifty Stylesheet [ REQUIRED ]-->
    <link href="../static/css/nifty.min.css" rel="stylesheet">

    <!--Nifty Premium Icon [ DEMONSTRATION ]-->
    <link href="../static/css/nifty-demo-icons.min.css" rel="stylesheet">

    <!--Demo [ DEMONSTRATION ]-->
    <link href="../static/css/nifty-demo-icons.min.css" rel="stylesheet">
    <link href="../static/css/nifty-demo.min.css" rel="stylesheet">
    <link href="../static/css/font-awesome.min.css" rel="stylesheet">

    <!--JAVASCRIPT-->
    <!--=================================================-->
    <!--Pace - Page Load Progress Par [OPTIONAL]-->
    <link href="../static/css/pace.min.css" rel="stylesheet">

    <script src="../static/js/pace.min.js"></script>

    <!--jQuery [ REQUIRED ]-->
    <script src="../static/js/jquery-2.2.4.min.js"></script>

    <!--BootstrapJS [ RECOMMENDED ]-->
    <script src="../static/js/bootstrap.min.js"></script>

    <!--NiftyJS [ RECOMMENDED ]-->
    <script src="../static/js/nifty.min.js"></script>
    <script src="../static/js/icons.js"></script>
    <script src="../static/js/echarts.min.js"></script>

    <!--Demo script [ DEMONSTRATION ]-->
    <script src="../static/js/nifty-demo.min.js"></script>
</head>

<body>

    <div id="container" class="effect aside-float aside-bright mainnav-sm">

        <!--NAVBAR-->
        <!--===================================================-->
        <header id="navbar">
            <div id="navbar-container" class="boxed">

                <!--Brand logo & name-->
                <!--================================-->
                <div class="navbar-header">
                    <a class="navbar-brand">
                        <img src="../static/images/logo.jpg" alt="Nifty Logo" class="brand-icon">
                        <div class="brand-title">
                            <span class="brand-text">《机器学习》</span>
                        </div>
                    </a>
                </div>
                <!--================================-->
                <!--End brand logo & name-->

                <!--Navbar Dropdown-->
                <!--================================-->
                <div class="navbar-content clearfix">
                    <ul class="nav navbar-top-links pull-left">

                        <!--Navigation toogle button-->
                        <!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
                        <li class="tgl-menu-btn">
                            <a class="mainnav-toggle" href="#">
                                <i class="demo-pli-view-list"></i>
                            </a>
                        </li>

                        <ul class="nav navbar-top-links pull-left">
                            <h4>《机器学习》知识点整理与展示</h4>
                        </ul>
                </div>
                <!--================================-->
                <!--End Navbar Dropdown-->
            </div>
        </header>
        <!--===================================================-->
        <!--END NAVBAR-->
        <!-- MAIN CONTENT -->
        <!--===================================================-->
        <div class="boxed">
            <div id="content-container">
                <div id="page-title">
                    <h1 class="page-header text-overflow">知识点全貌</h1>
                </div>
                <div class="container">
                    <div class="text-center">
                        <button class="btn btn-primary" onclick="showModal('add')">添加节点</button>
                        <button class="btn btn-success" onclick="showModal('relationship')">添加关系</button>
                        <button class="btn btn-danger" onclick="showModal('delete')">删除节点</button>
                    </div>
                </div>
                <!--Page content-->
                <!--===================================================-->
                <div id="page-content">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="panel">
                                <!-- <div class="panel-heading">
                                    <h3 class="panel-title">知识图谱</h3>
                                </div> -->
                                <div class="panel-body">
                                    <div id="graphContainer" style="width: 100%; height: 600px;"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 模态框 -->
                <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                        aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="myModalLabel">操作</h4>
                            </div>
                            <div class="modal-body">
                                <form id="dynamic-form">
                                    <!-- 节点名称 -->
                                    <div class="form-group">
                                        <label for="node-name-input">节点名称</label>
                                        <input type="text" class="form-control" id="node-name-input"
                                            placeholder="请输入节点名称">
                                    </div>
                                    <!-- 节点类型 -->
                                    <div class="form-group hidden" id="node-type-group">
                                        <label for="node-type-input">节点类型</label>
                                        <input type="text" class="form-control" id="node-type-input"
                                            placeholder="请输入节点类型">
                                    </div>
                                    <!-- 第二个节点名称 -->
                                    <div class="form-group hidden" id="relation-group">
                                        <label for="node-name-input2">目标节点名称</label>
                                        <input type="text" class="form-control" id="node-name-input2"
                                            placeholder="请输入目标节点名称">
                                    </div>
                                    <!-- 关系名称 -->
                                    <div class="form-group hidden" id="relation-name-group">
                                        <label for="node-relation-input">关系名称</label>
                                        <input type="text" class="form-control" id="node-relation-input"
                                            placeholder="请输入关系名称">
                                    </div>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                <button type="button" class="btn btn-primary" onclick="submitForm()">提交</button>
                            </div>
                        </div>
                    </div>
                </div>

                <footer id="footer">
                    <p class="pad-lft">Copyright&#0169; 20221113170 刘盛东 《机器学习》知识点整理与展示
                        &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;All Rights Reserved </p>
                </footer>

                <script>
                    function initGraph() {
                        fetch('http://127.0.0.1:5000/get_graph')
                            .then(response => response.json())
                            .then(response => {
                                var nodes = response.nodes;
                                var links = response.relationships;

                                // 使用ECharts绘制知识图谱
                                var myChart = echarts.init(document.getElementById('graphContainer'));

                                var option = {
                                    title: {
                                        text: '知识图谱',
                                        subtext: '《机器学习》知识点整理与展示',
                                        top: 'top',
                                        left: 'left'
                                    },
                                    tooltip: {},
                                    animationDurationUpdate: 1500,
                                    animationEasingUpdate: 'quinticInOut',
                                    series: [
                                        {
                                            type: 'graph',
                                            layout: 'force',
                                            symbolSize: 50,
                                            roam: true,
                                            label: {
                                                normal: {
                                                    show: true,
                                                    textStyle: {}
                                                }
                                            },
                                            edgeSymbol: ['circle', 'arrow'],
                                            edgeSymbolSize: [4, 10],
                                            edgeLabel: {
                                                fontSize: 12,
                                                show: true
                                            },
                                            data: nodes.map(function (node) {
                                                return {
                                                    name: node.label,
                                                    value: 10,
                                                    symbolSize: 50
                                                };
                                            }),
                                            links: links.map(function (link) {
                                                return {
                                                    source: link.source,
                                                    target: link.target,
                                                    label: {
                                                        show: true,
                                                        formatter: link.label
                                                    }
                                                };
                                            }),
                                            categories: nodes.map(function (node) {
                                                return node.label;
                                            }),
                                            force: {
                                                repulsion: 1000, // 调整节点之间的斥力
                                                edgeLength: [50, 200], // 边的长度范围
                                                gravity: 0.1, // 引力系数
                                            },
                                            draggable: true
                                        }
                                    ]
                                };

                                myChart.setOption(option);
                            })
                            .catch(error => {
                                console.error('Failed to fetch graph data:', error);
                            });
                    }
                    
                    function showModal(action) {
                        $('#myModal').data('action', action); // 存储当前操作类型

                        // 重置表单
                        $('#dynamic-form')[0].reset();

                        // 根据操作类型显示或隐藏输入框
                        if (action === 'add') {
                            $('#node-type-group').removeClass('hidden'); // 显示节点类型输入框
                            $('#relation-group, #relation-name-group').addClass('hidden'); // 隐藏关系相关输入框
                            $('#myModalLabel').text('添加节点');
                        } else if (action === 'relationship') {
                            $('#relation-group, #relation-name-group').removeClass('hidden'); // 显示关系相关输入框
                            $('#node-type-group').addClass('hidden'); // 隐藏节点类型输入框
                            $('#myModalLabel').text('添加关系');
                        } else if (action === 'delete') {
                            $('#node-type-group, #relation-group, #relation-name-group').addClass('hidden'); // 隐藏所有多余输入框
                            $('#myModalLabel').text('删除节点');
                        }

                        // 显示模态框
                        $('#myModal').modal('show');
                    }

                    // 提交表单
                    function submitForm() {
                        var action = $('#myModal').data('action'); // 获取当前操作类型

                        if (action === "add") {
                            const node_name = $("#node-name-input").val();
                            const node_type = $("#node-type-input").val();

                            fetch("http://127.0.0.1:5000/add_node", {
                                method: "POST",
                                headers: {
                                    "Content-Type": "application/json"
                                },
                                body: JSON.stringify({ node_name: node_name, node_type: node_type })
                            })
                                .then(response => {
                                    if (!response.ok) {
                                        throw new Error("节点添加失败！");
                                    }
                                    return response.json();
                                })
                                .then(data => {
                                    alert(data.message);
                                    location.reload();
                                })
                                .catch(error => {
                                    alert(error.message || "节点添加失败！");
                                    console.error("Error adding node:", error);
                                });
                        } else if (action === "relationship") {
                            const node_name = $("#node-name-input").val();
                            const node2_name = $("#node-name-input2").val();
                            const relation_name = $("#node-relation-input").val();

                            fetch("http://127.0.0.1:5000/add_relationship", {
                                method: "POST",
                                headers: {
                                    "Content-Type": "application/json"
                                },
                                body: JSON.stringify({ node_name: node_name, node2_name: node2_name, relation_name: relation_name })
                            })
                                .then(response => {
                                    if (!response.ok) {
                                        throw new Error("关系添加失败！");
                                    }
                                    return response.json();
                                })
                                .then(data => {
                                    alert(data.message);
                                })
                                .catch(error => {
                                    alert(error.message);
                                });
                        } else if (action === "delete") {
                            const node_name = $("#node-name-input").val();

                            fetch('http://127.0.0.1:5000/delete_node', {
                                method: "POST",
                                headers: {
                                    "Content-Type": "application/json"
                                },
                                body: JSON.stringify({ node_name: node_name })
                            })
                                .then(response => {
                                    if (!response.ok) {
                                        throw new Error("节点删除失败！");
                                    }
                                    return response.json();
                                })
                                .then(data => {
                                    alert(data.message);
                                    location.reload();
                                })
                                .catch(error => {
                                    alert(error.message || "节点删除失败！");
                                    console.error("Error deleting node:", error);
                                });
                        }
                    }
                </script>

                <nav id="mainnav-container">
                    <div id="mainnav">

                        <!--Menu-->
                        <!--================================-->
                        <div id="mainnav-menu-wrap">
                            <div class="nano">
                                <div class="nano-content">

                                    <ul id="mainnav-menu" class="list-group">
                                        <!--Category name-->
                                        <li class="list-header"> 知识点可视化</li>

                                        <!--Menu list item-->

                                        <li>
                                            <a href="查询页面.html">
                                                <i class="fa fa-search" style="width:24px"></i>
                                                <span class="menu-title">
                                                    <strong>检索知识点</strong>
                                                </span>

                                            </a>
                                        </li>

                                        <li>
                                            <a href="展示页面.html">
                                                <i class="fa fa-pie-chart" style="width:24px"></i>
                                                <span class="menu-title">
                                                    <strong>知识点全貌</strong>
                                                </span>

                                            </a>
                                        </li>

                                        <li>
                                            <a href="增删改查页面.html">
                                                <i class="fa fa-pie-chart" style="width:24px"></i>
                                                <span class="menu-title">
                                                    <strong>增删改知识点</strong>
                                                </span>

                                            </a>
                                        </li>
                                        <!--Menu list item-->

                                        <li class="list-divider"></li>

                                        <!--Category name-->
                                        <li class="list-header">问答系统</li>

                                        <!--Menu list item-->
                                        <li>
                                            <a href="{{ url_for('KGQA') }}">
                                                <i class="fa fa-question-circle" style="width:24px"></i>
                                                <span class="menu-title">知识点问答</span>

                                            </a>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                        <!--================================-->
                        <!--End menu-->
                    </div>
                </nav>
                <!--===================================================-->
                <!--END MAIN NAVIGATION-->
            </div>
            <!-- FOOTER -->
            <!--===================================================-->
            <footer id="footer">

                <p class="pad-lft">Copyright&#0169; 20221113170 刘盛东 《机器学习》知识点整理与展示 &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;All
                    Rights
                    Reserved </p>
            </footer>
            <!--===================================================-->
            <!-- END FOOTER -->

            <!-- SCROLL PAGE BUTTON -->
            <!--===================================================-->
            <button class="scroll-top btn">
                <i class="pci-chevron chevron-up"></i>
            </button>
            <!--===================================================-->
        </div>
        <!--===================================================-->
        <!-- END OF CONTAINER -->
</body>

</html>